package cn.itsource.pethome.system.pet.controller;

import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.jwt.LoginContextUtils;
import cn.itsource.pethome.system.pet.domain.SearchMasterVo;
import cn.itsource.pethome.system.pet.service.ISearchMasterMsgService;
import cn.itsource.pethome.system.pet.domain.SearchMasterMsg;
import cn.itsource.pethome.system.pet.query.SearchMasterMsgQuery;
import cn.itsource.pethome.system.user.domain.Employee;
import cn.itsource.pethome.utils.AjaxResult;
import cn.itsource.pethome.utils.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private LoginContextUtils loginContextUtils;

    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody SearchMasterMsgQuery query)
    {
        try {
            List<SearchMasterMsg> byQuery = searchMasterMsgService.findByQuery(query);
            Long count = searchMasterMsgService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            searchMasterMsgService.publish(searchMasterMsg,request);
           return AjaxResult.success();
        }catch (BusinessException b){
            return AjaxResult.error().setMsg(b.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @GetMapping("/myCenter")
    public AjaxResult myCenter(HttpServletRequest request){
        try {
            List<SearchMasterMsg> masterMsgList = searchMasterMsgService.myCenter(request);
            return AjaxResult.success().setData(masterMsgList);
        }catch (BusinessException b){
            return AjaxResult.error().setMsg(b.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    /**
     * 寻主池
     * @param query
     * @return
     */
    @PostMapping("/toSearchMasterPool")
    public AjaxResult toSearchMasterPool(@RequestBody SearchMasterMsgQuery query){
        try {
            query.setShopIsExist(true);//开启
            List<SearchMasterMsg> byQuery = searchMasterMsgService.findByQuery(query);
            Long count = searchMasterMsgService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    /**
     * 待处理
     * @param query
     * @return
     */
    @PostMapping("/toHandle")
    public AjaxResult toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            Employee employee = (Employee)loginContextUtils.getLoginUser(request);
            query.setShopId(employee.getShopId());
            query.setState(BaseConstants.Publish.STATE_PASS);
            List<SearchMasterMsg> byQuery = searchMasterMsgService.findByQuery(query);
            Long count = searchMasterMsgService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    /**
     * 已完成
     * @param query
     * @return
     */
    @PostMapping("/finish")
    public AjaxResult finish(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            Employee employee = (Employee)loginContextUtils.getLoginUser(request);
            query.setShopId(employee.getShopId());
            query.setState(BaseConstants.Publish.STATE_OK);
            List<SearchMasterMsg> byQuery = searchMasterMsgService.findByQuery(query);
            Long count = searchMasterMsgService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @GetMapping("/orderTaking/{id}")
    public AjaxResult orderTaking(@PathVariable("id") Long id,HttpServletRequest request){
        try {
            Employee employee = (Employee)loginContextUtils.getLoginUser(request);
            searchMasterMsgService.orderTaking(id,employee.getShopId());
            return AjaxResult.success();
        }catch (BusinessException b){
            return AjaxResult.error().setMsg(b.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @PostMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.reject(id);
            return AjaxResult.success();
        }catch (BusinessException b){
            return AjaxResult.error().setMsg(b.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody SearchMasterVo searchMasterVo){
        try {
            //做什么事件
            searchMasterMsgService.accept(searchMasterVo);
            return AjaxResult.success();
        }catch (BusinessException b){
            return AjaxResult.error().setMsg(b.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }



}
